import java.util.Arrays;

public class ValidTriangleNumber {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        int sum = 0;
        for(int c = n - 1; c >= 2; c--) {
            int left = 0, right = c - 1;
            while(left < right) {
                if(nums[left] + nums[right] > nums[c]) {
                    sum += right - left;
                    right--;
                }else {
                    // nums[left] + nums[right] <= c
                    left++;
                }
            }
        }
        return sum;
    }
}
